﻿//Design By Do Trung --- Email : trungqn19852003@yahoo.com
//Ngày Design :29/03/2012 10:34:13 AM
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using Savico.Library.Commom.SqlServer;
using Entity = Savico.Library.Entity;
namespace Savico.Library.Data
{
    public class TaoMa
    {
        public TaoMa() { }
        Connection db = new Connection();
        public bool Update_Ma(Entity.TaoMa entity)
        {
            bool boolUpdate = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_TaoMaTuDong_Update";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@LoaiMa", SqlDbType.Int).Value = entity.LoaiMa;                

                SqlAdp.UpdateCommand = SqlCmd;
                SqlAdp.UpdateCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolUpdate = true;
                SqlCnn.Close();
            }
            catch
            {
                boolUpdate = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolUpdate;
        }
        public int TaoMaMoi(Entity.TaoMa entity)
        {
            int NewId = 0;
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_TaoMaTuDong";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@LoaiMa", SqlDbType.NVarChar ,20).Value = entity.LoaiMa;
                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                NewId = Convert.ToInt32(dt.Rows[0][0].ToString().Trim());
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return NewId;
        }
    }
}
